//轮播图列表展示功能
$.ajax({
    type:'get',
    url:'/slides',
    success:function(res){
        console.log(res)
        var html = template('slidesTpl',{data:res})
        $('#slidesBox').html(html)
    }
})

//实现图片上传功能
$('#file').on('change',function(){
    var formData = new FormData()
    formData.append('image',this.files[0])
    $.ajax({
        type:'post',
        url:'/upload',
        data:formData,
        contentType:false,
        processData:false,
        success:function(res){
            console.log(res)
            $('#hiddenImg').val(res[0].image)
            $('#preview').attr('src',res[0].image).show()
        }
    })
})

//添加功能
$('#slidesForm').on('submit',function(){
    var data = $(this).serialize()
    $.ajax({
        type:'post',
        url:'/slides',
        data:data,
        success:function(res){
            location.reload()
        }
    })
    return false;
})

//实现删除功能
$('#slidesBox').on('click','.delete',function(){
    var id = $(this).attr('data-id')
    $.ajax({
        type:'delete',
        url:`/slides/${id}`,
        beforeSend:function(){
            if(!confirm('确定要删除吗?')){
                return false;//取消ajax的发送
            }
        },
        success:function(res){
            location.reload()
        }
    })
})